package com.hspedu.tankgame5;


/**
 * @author LiuJinTao
 * @className Launch
 * @date 2024/06/24 9:56
 * @description TODO
 */
public class Shot implements Runnable {
    int x;
    int y;
    int direct;
    int speed = 2;
    boolean isLive = true;


    public Shot(int x, int y, int direct) {
        this.x = x;
        this.y = y;
        this.direct = direct;
    }

    public void moveUp() {
        y -= speed;
    }

    public void moveRight() {
        x += speed;
    }

    public void moveDown() {
        y += speed;
    }

    public void moveLeft() {
        x -= speed;
    }

    @Override
    public void run() {// 射击
        while (true) {
            // 休眠 50 毫秒
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            // 根据方向改变 x,y 坐标
            switch (direct) {
                case 0:
                    moveUp();
                    break;
                case 1:
                    moveRight();
                    break;
                case 2:
                    moveDown();
                    break;
                case 3:
                    moveLeft();
                    break;
            }

            // 结束线程
            if (!(x >= 0 && x <= 1000 && y >= 0 && y <= 750) || !isLive) {
                isLive = false;
                break;
            }
        }
    }


}
